/**
 * 定义分钟数据模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.common.report.reportBase', ['water.entity.monitorData',
	                                                                              'OnlineMonitorDataReportService',
	                                                                              'water.common.dateUtil']);
	
	module.controller('commonReportController',function($rootScope,$scope,$monitorDataService,$DataReportService,$dateUtil){

		/**
		 * 开始小时
		 */
		$scope.startHour = new Date().getHours()-1;
		/**
		 * 结束小时
		 */
		$scope.endHour = new Date().getHours()+1;
		/**
		 * 小时集合，界面下拉框的数据源
		 */
		$scope.hours = [];
		/**
		 * 界面上冻结表格的组件
		 */
		$scope.freezeGrid;
		/**
		 * 查询日期,初始化成当前时间
		 */
		$scope.queryDate = $dateUtil.formatDate(new Date(),'yyyy-MM-dd');
		/**
		 * 数据日期分页查询器
		 */
		$scope.dataGetter;
		$scope.dataAPIName;
		$scope.dataExportAPIName;
		
		/**
		 * 图表污染物参数是否默认选前四个
		 */
		var pollFreshLag=true;
		
		for(var i=0;i<24;i++){
			$scope.hours.push({
				id:i
			});
		}
		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
			pollFreshLag=true;
			$scope.onTBClick();
	    });
		
		/**
		 * 切换菜单的时候，把注册的事件取消掉
		 */
		$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
			$scope.oNdataPramChanged();
	 	});
		
		/**
		 * 站点选择后，报表查询前执行的逻辑
		 * 用于准备查询前的参数
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
			$scope.parmList = $scope.selectedModel.parmToCheckList;
		}
		
		/**
		 * 查询按钮点击
		 */
		$scope.onSearchClick = function(){
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
		}
		
		/**
		 * 图表选项卡点击事件
		 */
		$scope.onTBClick = function(){
			if(pollFreshLag) {
				for(var i=0;i<$scope.parmList.length;i++){
					if(i <= 3){
						$scope.parmList[i].checked = true;
					} else {
						$scope.parmList[i].checked = false;
					}
				}
				pollFreshLag=false;
			}
			_showChar();
		}
		
		/**
		 * 导出excel
		 */
		$scope.onExportExcelClick = function(){
			var queryObj = $scope.getQueryObject(true);
			$monitorDataService[$scope.dataExportAPIName](queryObj);
		}
		
		/**
		 * 导出excel
		 */
		$scope.onExportExcelDaily = function(){
			var queryObj = $scope.getQueryObject(true);
			$monitorDataService.exportDailyExcel(queryObj);
		}
		
		/**
		 * 查询报表数据
		 * 逻辑是，根据查询条件构建数据查询器，这个查询器就提供了分页功能
		 * 如果条件没有变化，就拿这个查询器进行上一页下一页操作
		 * 如果条件有变化，就重新初始化这个查询器
		 */
		$scope.getReport = function(queryObject){
			if(!$scope.dataAPIName){
				$scope.dataAPIName = "realTimeListGetter";
			}
			/**
			 * 获取日期分页的实时数据查询器，方便日期分页查询
			 * 页面上可以直接调用该对象的分页方法
			 */
			$scope.dataGetter = $monitorDataService[$scope.dataAPIName](queryObject,function(data){
				$scope.data = data;
				/**
				/**
				/**
				 * 如果没有数据，直接启动窗口冻结，不然需要等很久
				 */
				if($scope.freezeGrid && data.data.length < 1){
					$scope.freezeGrid.freezeGridWithoutWait();
				}
				$scope.onTBClick();
			});
			$scope.dataGetter.nextPage();
		};
		
		/**
		 * 获取查询参数
		 * forExcel 是否用于导出
		 */
		$scope.getQueryObject = function(forExcel){
			var startTimeStr =$scope.queryDate + " " + $scope.startHour + ":00:00";
			var endTimeStr = $scope.queryDate + " " + $scope.endHour + ":59:59";
			
			var parmsStr = $scope.selectedModel.parmToChecksStr;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
					"PARMS":parmsStr,
					"START":startTimeStr,
					"END":endTimeStr,
					"EXPORT":forExcel
				};
			return queryObject;
		}
		
		/**
		 * 图表视图，勾选参数事件
		 */
		$scope.checkedParm = function($event,parmIndex){
			var checkedCount = 0;
			var checkBox = $event.target;
			for(var i=0;i<$scope.parmList.length;i++){
				if($scope.parmList[i].checked){
					checkedCount++;
				}
			}
			if(checkBox.checked && checkedCount >=4){
				checkBox.checked = false;
				$.messager.alert("提示","只能选择4个参数","info");
				return;
			}
			$scope.parmList[parmIndex].checked = checkBox.checked;
			_showChar();
		}
		
		/**
		 * 展示图表
		 */
		function _showChar(){
			var dataObject= {};
			dataObject.mn = $scope.selectedModel.MN;
			dataObject.statisType=$scope.statisType;
			var parmsToChar = [];
			var waterType=getWaterType();
			for(var i=0;i<$scope.parmList.length;i++){
				if($scope.parmList[i].checked){
					parmsToChar.push($scope.parmList[i]);
				}
			}
			if($scope.data){
				//在绘制
				$DataReportService.showChar_for_new($scope.data.data,'char_tb',parmsToChar,$scope.data.pollInfo,dataObject,waterType);	
			}
			
		}
		
		$("#listdata").idTabs();
		$("#lb").click();		
	});
}(window, angular));	